package com.bestcem.xm.member.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.bestcem.xm.common.core.uitls.StringUtil;
import com.bestcem.xm.member.dao.GroupDao;
import com.bestcem.xm.member.dao.mapper.GroupMapper;
import com.bestcem.xm.member.entity.mysql.GroupEntity;
import com.bestcem.xm.member.entity.pojo.GroupDO;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 示例服务
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/12/24
 */
@Repository("groupDao")
public class GroupDaoImpl implements GroupDao {

    @Resource
    private GroupMapper groupMapper;

    @Override
    public Long countByOrgIdAndIsSample(String orgId) {
        return groupMapper.countByOrgIdAndIsSample(orgId);
    }

    @Override
    public Long countByOrgIdAndName(String orgId, String name) {
        return groupMapper.countByOrgIdAndName(orgId, name);
    }

    @Override
    public GroupDO save(GroupDO group) {
        // 设置id
        group.setId(StringUtil.getId());
        groupMapper.createGroup(GroupDO.toEntity(group));
        GroupEntity entity = groupMapper.findById(group.getId());
        if (Objects.nonNull(entity)) {
            return GroupDO.toDO(entity);
        }
        return null;
    }

    @Override
    public GroupDO findById(String id) {
        GroupEntity entity = groupMapper.findById(id);
        if (ObjectUtil.isNotEmpty(entity)) {
            return GroupDO.toDO(entity);
        }
        return null;
    }

    @Override
    public void updateGroup(GroupDO group) {
        groupMapper.updateGroup(GroupDO.toEntity(group));
    }

    @Override
    public List<GroupDO> findByOrgId(String orgId) {
        List<GroupEntity> entities = groupMapper.findByOrgId(orgId);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(GroupDO::toDO).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public void deleteById(String id) {
        groupMapper.deleteById(id);
    }

    @Override
    public List<GroupDO> listByOrgIdAndAuto(String orgId, int auto) {
        List<GroupEntity> entities = groupMapper.listByOrgIdAndAuto(orgId, auto);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(GroupDO::toDO).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public void updateGroupSyncToken(String groupId, String syncToken, Date commonDate) {
        groupMapper.updateGroupSyncToken(groupId, syncToken, commonDate);
    }

    @Override
    public GroupDO findOneByUpdateTimeGt(String orgId, Date updateTime) {
        GroupEntity entity = groupMapper.findOneByUpdateTimeGt(orgId, updateTime);
        if (ObjectUtil.isNotEmpty(entity)) {
            return GroupDO.toDO(entity);
        }
        return null;
    }

    @Override
    public void updateSyncTokenByIdAndSyncToken(String groupId, String newSyncToken, String oldSyncToken, Date commonDate) {
        groupMapper.updateSyncTokenByIdAndSyncToken(groupId, newSyncToken, oldSyncToken, commonDate);
    }
}
